/**
 * 
 */
package com.neu.mapreduce.tweet;

import java.text.DateFormat;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;

/**
 * @author hduser
 * 
 */
public class Util {

	public static Date createDate(String s) throws ParseException  {

		String[] sArray = s.split(" ");
		String year = sArray[5];
		String datenum = sArray[2];
		String month = sArray[1];
		// String time = sArray[3];
		// String[] timeArray = time.split(":");
		/*
		 * int hrs = Integer.parseInt(timeArray[0]); int mins =
		 * Integer.parseInt(timeArray[1]); int secs =
		 * Integer.parseInt(timeArray[2]);
		 */

		Map<String, String> m = new HashMap<String, String>();

		m.put("Jan", "01");
		m.put("Feb", "02");
		m.put("Mar", "03");
		m.put("Apr", "04");
		m.put("May", "05");
		m.put("Jun", "06");
		m.put("Jul", "07");
		m.put("Aug", "08");
		m.put("Sep", "09");
		m.put("Oct", "10");
		m.put("Nov", "11");
		m.put("Dec", "12");

		DateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		java.util.Date d1 = df.parse(year + "-" + m.get(month) + "-" + datenum);

		return d1;
	}

	public static int compareDates(Date d1, Date d2) {

		if (d1.equals(d2))
			return 0;
		else if (d1.before(d2))
			return -1;
		else
			return 1;

	}

	public static int differenceDates(Date startDate, Date endDate) {

		final long MILLISECONDS_PER_DAY = 24 * 60 * 60 * 1000;
		long diff = endDate.getTime() - startDate.getTime();
		int days = (int) Math.floor(diff / MILLISECONDS_PER_DAY);
		return Math.abs(days);
	}

}
